package com.crm.service.impl.opportunityManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.opportunityManager.OpportunityProductMapper;
import com.crm.dao.mapper.opportunityManager.PhraseMapper;
import com.crm.model.entity.opportunityManager.OpportunityProduct;
import com.crm.model.entity.opportunityManager.OpportunityProductExample;
import com.crm.model.entity.opportunityManager.Phrase;
import com.crm.model.entity.opportunityManager.PhraseExample;
import com.crm.service.opportunityManager.OpportunityPhraseDbService;
import com.crm.service.opportunityManager.OpportunityProductDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商机产品
 */
@Service
public class OpportunityProductDbServiceImpl implements OpportunityProductDbService {

    @Autowired
    private OpportunityProductMapper opportunityProductMapper;

    /**
     * 商机-新增商机-查询商机下的产品
     * @param opportunityId 商机id
     * @param order 分组
     * @return
     */
    @Override
    public List<OpportunityProduct> opportunityProductList(String opportunityId, String order,Integer whetherRenew) {
        OpportunityProductExample opportunityProductExample = new OpportunityProductExample();
        OpportunityProductExample.Criteria criteria = opportunityProductExample.createCriteria();
        opportunityProductExample.setOrderByClause(order);
        criteria.andOpportunityIdEqualTo(opportunityId);
        if(whetherRenew != null) {
            criteria.andWhetherRenewEqualTo(whetherRenew);
        }
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<OpportunityProduct> list = opportunityProductMapper.selectByExample(opportunityProductExample);
        return list;
    }

    /**
     * 商机管理-新增商机产品
     * @param opportunityProduct 商机产品实体
     * @return
     */
    @Override
    public Boolean addOpportunityProduct(OpportunityProduct opportunityProduct) {
        Boolean result = false;
        int num = opportunityProductMapper.insertSelective(opportunityProduct);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 商机-商机详情-修改商机产品回显
     * @param opportunityProductId 商机产品id
     * @return
     */
    @Override
    public OpportunityProduct getOpportunityProductById(String opportunityProductId) {
        return opportunityProductMapper.selectByPrimaryKey(opportunityProductId);
    }

    /**
     * 商机管理-修改商机产品
     * @param opportunityProduct 商机产品实体
     * @return
     */
    @Override
    public Boolean updateOpportunityProduct(OpportunityProduct opportunityProduct) {
        Boolean result = false;
        int num = opportunityProductMapper.updateByPrimaryKeySelective(opportunityProduct);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 通过产品id和商机id查询是否有这个产品
     * @param customerProductId 产品id
     * @param opportunityId 商机id
     * @return
     */
    @Override
    public List<OpportunityProduct> opportunityCodeAndOppIdList(String customerProductId, String opportunityId) {
        OpportunityProductExample opportunityProductExample = new OpportunityProductExample();
        OpportunityProductExample.Criteria criteria = opportunityProductExample.createCriteria();
        criteria.andCustomerProductIdEqualTo(customerProductId);
        criteria.andOpportunityIdEqualTo(opportunityId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<OpportunityProduct> list = opportunityProductMapper.selectByExample(opportunityProductExample);
        return list;
    }
}
